<template>
  <div class="groupBuyBox sub-main">
    <ActivitySlot class="activityGroup" :imgTitle="imgTitle">
      <template #title>
        <div class="title">
          <div class="top"></div>
          <div class="bottom">
            <span class="glodTxt" v-if="!ifPreheat">距离本场结束：</span>
            <span class="glodTxt" v-else>距离本场开始：</span>
            <div class="timeBox">
              <div class="hour bc">{{ formatHours(time) }}</div>
              <div class="after">:</div>
              <div class="min bc">{{ formatMinutes(time) }}</div>
              <div class="after">:</div>
              <div class="sec bc">{{ formatSeconds(time) }}</div>
            </div>
          </div>
        </div>
      </template>
      <template #section>
        <div class="groupList" v-if="flag">
          <div class="item" v-for="item in proList" :key="item.productId">
            <ItemSlot :data="item" @toDetail="toProductDetail(item)">
              <template #extra>
                <div class="progress">
                  <span>限量{{ 100 }}件</span>
                  <el-progress
                    :color="'#C83732'"
                    :show-text="false"
                    :stroke-width="26"
                    :percentage="70" />
                </div>
              </template>
              <template #icon>
                <svg-icon style="width: 57px;height: 57px;" name="activity-pintuan" />
              </template>
              <template #button>
                <button class="buy" :class="ifPreheat ? 'buyWill' : 'butNow'" @click="toProductDetail(item)">{{ ifPreheat ? '未开始' : '立即拼团' }}</button>
              </template>
            </ItemSlot>
          </div>
        </div>
        <div v-else class="noproduct">
          <svg-icon
            style="width: 240px; height: 240px; margin-bottom: 20px;"
            name="user-order-nodata"
          />
          <p class="fs20 font-color-999">该活动没有商品～</p>
        </div>
      </template>
    </ActivitySlot>
    <el-pagination
      v-if="flag"
      style="margin-bottom:20px;"
      background
      layout="prev, pager, next, jumper"
      :page-size="pageSize"
      :current-page="page"
      @current-change="changePage"
      :total="total" />
  </div>
</template>

<script setup>
import ActivitySlot from '@/components/activity/activitySlot.vue'
import ItemSlot from '@/views/promotions/components/itemSlot.vue'
import SvgIcon from '@/components/svgIcon/index.vue';
import {
  getGroupList
} from '@/api/Activity/ActivityGroup.js'
import { defineComponent, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useLoading } from '@/hooks/useLoading'

defineComponent({
  name:'GroupBuy'
})

const {showLoading,closeLoading} = useLoading()

const imgTitle = ref('static/image/activity/groupTitle.webp')
const page = ref(1)
const pageSize = ref(12)
const total = ref(0)
const id = ref('')
const shopId = ref('')
const ifPreheat = ref(false)
const time = ref('')
const setTime = ref(null)
const proList = ref([])
const volume = ref('')
const type = ref('')
const index = ref('1')
const flag = ref(true)

function formatHours (value) {
  if (!value) { return '00' }
  let hours = Math.floor(value / (1000 * 60 * 60))
  if (hours < 10) {
    return '0' + hours
  } else {
    return hours
  }
}

function formatMinutes (value) {
  if (!value) { return '00' }
  let minutes = Math.floor((value % (1000 * 60 * 60)) / (1000 * 60))
  if (minutes < 10) {
    return '0' + minutes
  } else {
    return minutes
  }
}

function formatSeconds (value) {
  if (!value) { return '00' }
  let seconds = Math.round((value % (1000 * 60)) / 1000)
  if (seconds < 10) {
    return '0' + seconds
  } else {
    return seconds
  }
}

const route = useRoute()
const router = useRouter()
onMounted(()=>{
  if (route.query.id) {
    id.value = route.query.id
  }
  if (route.query.shopId) {
    shopId.value = route.query.shopId
  }
  getWork()
})

// 跳转到商品详情
function toProductDetail (item) {
  let data = {
    productId: item.productId,
    shopId: item.shopId,
    skuId: item.skuId
  }
  router.push({
    path: '/productDetail',
    query: {
      proData: JSON.stringify(data)
    }
  })
}

// 获取拼团专区列表
async function getWork () {
  showLoading()
  let params = {
    shopId: shopId.value,
    shopGroupWorkId: id.value,
    page: page.value,
    pageSize: pageSize.value
  }
  if (id.value === '') {
    params.volume = volume.value
    params.type = type.value
  }
  const response = await getGroupList(params)
  const res = response.data
  if (res.code === '200') {
    ifPreheat.value = res.data.state !== 1
    proList.value = res.data.page.list
    total.value = parseInt(res.data.page.total)
    time.value = ifPreheat.value ? res.data.enableTime : res.data.time
    // shopName = res.data.shopName
    // shopLogo = res.data.shopLogo
    if (res.data.page.list.length) {
      flag.value = true
    } else {
      flag.value = false
    }
    closeLoading()
  } else {
    ElMessage.warning(res.message)
  }
}

// 翻页
function changePage (val) {
  page.value = val
  getWork()
}
// 默认
function defaultSort () {
  index.value = '1'
  page.value = 1
  type.value = ''
  volume.value = ''
  getWork()
}
// 价格
function priceSort () {
  index.value = '2'
  page.value = 1
  if (type.value === '' || type.value === 2) {
    type.value = 1
  } else {
    type.value = 2
  }
  volume.value = ''
  getWork()
}

// 销量
function salesSort () {
  index.value = '3'
  page.value = 1
  if (volume.value === '' || volume.value === 2) {
    volume.value = 1
  } else {
    volume.value = 2
  }
  type.value = ''
  getWork()
}
</script>

<style lang="scss" scoped>
$zhekouHeight: 40px;
$zhekouTitleFS: 16px;
.groupBuyBox{
  max-width: 1250px;
  margin: auto;
  .activityGroup{
    position: relative;
    // .title{
    //   width: 40%;
    //   height: 60px;
    //   color: #FFF;
    //   margin: auto;
    //   text-align: center;
    //   position: absolute;
    //   top: 160px;
    //   left: 0;
    //   right: 0;
    //   display: flex;
    //   justify-content: space-around;
    //   align-items: center;
    //   .glodTxt{
    //     color: #FFEBC4;
    //     font-size: 30px;
    //     font-family: Microsoft YaHei;
    //   }
    //   .timeBox{
    //     height: 60px;
    //     display: flex;
    //     align-items: center;
    //     .after{
    //       font-size: 30px;
    //       margin: 0 13px;
    //     }
    //     .bc{
    //       width: 60px;
    //       height: 60px;
    //       line-height: 60px;
    //       font-size: 30px;
    //       font-family: Microsoft YaHei;
    //       font-weight: bold;
    //       color: #FFFFFF;
    //       background-color: #333333;
    //     }
    //   }
    // }
    .title{
      width: 40%;
      height: 100px;
      color: #FFF;
      margin: auto;
      text-align: center;
      position: absolute;
      top: 130px;
      left: 0;
      right: 0;
      .top{
        width: 146px;
        height: 60px;
        margin: auto;
        text-align: center;
        color: #C83732;
        font-size: 32px;
        font-family: Microsoft YaHei;
      }
      .bottom{
        display: flex;
        justify-content: center;
        align-items: center;
        .glodTxt{
          color: #FFEBC4;
          font-size: $zhekouTitleFS;
          font-family: Microsoft YaHei;
        }
        .timeBox{
          height: $zhekouHeight;
          display: flex;
          align-items: center;
          .after{
            font-size: $zhekouTitleFS;
            margin: 0 13px;
          }
          .bc{
            width: $zhekouHeight;
            height: $zhekouHeight;
            line-height: $zhekouHeight;
            font-size: $zhekouTitleFS;
            font-family: Microsoft YaHei;
            font-weight: bold;
            color: #FFFFFF;
            background-color: #333333;
          }
        }
      }
    }
    .groupList{
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      .item {
        width: 23%;
        margin: 20px 1%;
        transition: all 0.3s linear;
        .progress{
          width: 100%;
          height: 40px;
          background-color: #DADBDC;
          position: absolute;
          bottom: 0;
          display: flex;
          align-items: center;
          span{
            margin-left: 10px;
          }
        }
        .buy{
          width: 100%;
          height: 40px;
          padding: 0;
          margin: 15px 0 0;
          background: linear-gradient(90deg, #C83732 0%, #E25C44 100%);
          box-shadow: 0px 3px 6px rgba(233, 0, 0, 0.3);
          cursor: pointer;
        }
        .butNow{
          background: linear-gradient(90deg, #C83732 0%, #E25C44 100%);
          box-shadow: 0px 3px 6px rgba(233, 0, 0, 0.3);
        }
        .buyWill{
          background: linear-gradient(90deg, #29C790 0%, #75D98C 100%);
          box-shadow: 0px 3px 6px rgba(52, 203, 144, 0.3);
        }
      }
      .item:hover{
        box-shadow:0 0 20px #cccccc;
      }
    }
  }
  .noproduct{
    width: 100%;
    text-align: center;
    min-height: 500px;
    img{
      margin-top: 50px;
    }
  }
}
</style>
